﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace BuildHelper.Work.Tools
{
    public partial class FormErrors : XtraForm
    {
        public Entry ErrorEntry
        {
            get;
            set;
        }
        public FormErrors()
        {
            InitializeComponent();
        }
        public FormErrors(List<Error> errors)
        {
            InitializeComponent();
            gridControl1.DataSource = errors;
            repItemBlame.Buttons[0].Image = Properties.Resources.tortoise_bl24.ResizeImage(16, 16);
            repositoryItemButtonEdit2.Buttons[0].Image = commonImageCollectionFull1.Images[33].ResizeImage(16, 16);
            repositoryItemButtonEdit1.Buttons[0].Image = commonImageCollectionFull1.Images[30].ResizeImage(16, 16);
            repItemLog.Buttons[0].Image = Properties.Resources.tortoise_log24.ResizeImage(16, 16);
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFile();
        }
        private void OpenFile()
        {
            object obj = gridView1.GetRow(gridView1.FocusedRowHandle);
            if (obj != null)
            {
                if (obj != null && obj is Error)
                {
                    Error err = obj as Error;
                    OpenError(err, ErrorEntry);
                }
            }
        }

        public static void OpenError(Error err, Entry ErrorEntry)
        {
            switch (ErrorEntry)
            {
                case Entry.Gui:
                    GlobalTools.FilesTools.OpenFileInDevEnv(err.Plik, err.Linia, err.Kolumna);
                    break;
                case Entry.Java:
                    Process p = GlobalTools.FilesTools.OpenFile(EditorSource.OperacjeNaPlikach, err.Plik);
                    if (p != null)
                    {
                        Action a = new Action(() => { SendLine(p, err.Linia, err.Plik); });
                        a.BeginInvoke(null, null);
                    }
                    break;
                default:
                    break;
            }
        }

        private static void SendLine(Process p, int linia, string file)
        {
            try
            {
                if (p.HasExited)
                    p = User32.GetActiveWindowProcess();
                string name = Path.GetFileName(file);
                int i = 0;
                bool send = false;
                string text;
                while (i < 10)
                {
                    text = Win32.GetWindowText(User32.GetForegroundWindow());
                    if (text.XMatch(name))
                    {
                        send = true;
                        break;
                    }
                    Thread.Sleep(25);
                    i++;
                }
                if (send)
                {
                    WindowsInput.InputSimulator.SimulateModifiedKeyStroke(WindowsInput.VirtualKeyCode.CONTROL, WindowsInput.VirtualKeyCode.VK_G);
                    System.Threading.Thread.Sleep(50);
                    WindowsInput.InputSimulator.SimulateTextEntry(linia.ToString());
                    WindowsInput.InputSimulator.SimulateKeyPress(WindowsInput.VirtualKeyCode.EXECUTE);
                }
            }
            catch { }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            OpenFile();
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                OpenFile();
        }

        private void repItemBlame_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            object obj = gridView1.GetRow(gridView1.FocusedRowHandle);
            if (obj != null)
            {
                if (obj != null && obj is Error)
                {
                    Error err = obj as Error;
                    GlobalTools.TortoiseTools.ShowTortoiseBlameForFile(err.Plik);
                }
            } 
        }

        private void repositoryItemButtonEdit2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            object obj = gridView1.GetRow(gridView1.FocusedRowHandle);
            if (obj != null)
            {
                if (obj != null && obj is Error)
                {
                     Error err = obj as Error;
                     GlobalTools.FilesTools.OpenExplorerAndSelect(err.Plik);
                }
            }
        }

        private void repItemLog_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            object obj = gridView1.GetRow(gridView1.FocusedRowHandle);
            if (obj != null)
            {
                if (obj != null && obj is Error)
                {
                    Error err = obj as Error;
                    GlobalTools.TortoiseTools.ShowTortoiseLogForFile(err.Plik);
                }
            } 
        }
    }

    public enum Entry
    {
        Gui,
        Java
    }
    public class Error
    {
        public string Plik
        {
            get;
            set;
        }
        public int Linia
        {
            get;
            set;
        }
        public string Blad
        {
            get;
            set;
        }
        public int Kolumna
        {
            get;
            set;
        }
    }
}
